using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.IO;
using System.Data;


namespace SharpPopulatorLib
{

	public class Populator
    {

        #region Constants

        private String _SQLInsert = "INSERT INTO {0} ({1}) VALUES ({2})";

        #endregion

        #region Atributes

        private Int32 _NumberOfRecords;
		private DateTime _UsingDate;
		private Int16 _PasswordCharLength = 8;
        private ConfiguredFieldCollection _ConfiguredFields;
        private DataTable _dtCache;
        private StreamWriter _Scripts;
            
        #endregion

        #region Properties

        [Category("Settings"), Description("")]
        public Int32 NumberOfRecords
		{
			get { return _NumberOfRecords; }
			set { _NumberOfRecords = value; }
		}

        [Category("Settings"), Description("")]
		public DateTime UsingDate
		{
			get { return _UsingDate; }
			set { _UsingDate = value; }
		}

        [Category("Settings"), Description("")]
		public Int16 PasswordCharLength
		{
            get { return _PasswordCharLength; }
            set { _PasswordCharLength = value; }
		}

        [Category(""), Description("")]
        public ConfiguredFieldCollection ConfiguredFields
        {
            get { return _ConfiguredFields; }
            set { _ConfiguredFields = value; }
        }

        [Category(""), Description("")]
        public String TableName { get; set; }

        #endregion

        #region Constructors

        public Populator()
		{
			
		}

        #endregion

        #region Private Methods

        //private void BuildScript(String TableName, List<DBField> Fields)
        //{ 
        //    //
       
        //}

        //private void BuildCache()
        //{ 
        //    //To build a DataTable as Cache
        //    _dtCache = new DataTable(TableName);

        //    foreach (ConfiguredField _Field in _ConfiguredFields.ConfiguredFields)
        //    {
        //        _dtCache.Columns.Add(_Field.FieldName, typeof(System.String));
        //    }
        //}

        #endregion

        #region Public Methods

        /// <summary>
        /// 
        /// </summary>
        public void Populate()
        { 
            //
            StringBuilder _ListFields = new StringBuilder();
            StringBuilder _ListValues = new StringBuilder();
            List<Int32> _ListInt  = null;
            Int32 _i = 0;
            String FileName = String.Format("{0}.sql", TableName);

            try
            {
                if (_ConfiguredFields.Count > 0)
                {
                    _Scripts = new StreamWriter(FileName);
                    for (Int32 _j = 0 ; _j <= _NumberOfRecords ; _j++ )
                    {
                        foreach (ConfiguredField _Field in _ConfiguredFields.ConfiguredFields)
                        {

                            if (!_Field.PrimaryKey)
                            { _ListFields.Append(String.Format("{0}, ", _Field.FieldName)); }
                            
                            switch (_Field.Type)
                            {
                                case ConfigurationPropertiesType.Date:
                                 {
                                    DateProperties _DatePropr = (DateProperties)_Field.Configuration;
                                    if (_DatePropr.UseCustomDate)
                                    { _ListValues.Append(String.Format("'{0}', ", _DatePropr.CustomDate.ToShortDateString())); }
                                    else
                                    { _ListValues.Append(String.Format("'{0}', ", DateTime.Now.ToShortDateString())); }
                                }
                                break;
                                case ConfigurationPropertiesType.Domain:
                                {
                                    DomainProperties _DomainProps = (DomainProperties)_Field.Configuration;


                                    switch(_DomainProps.DomainName)
                                    {
                                        case DomainType.Custom:
                                        { _ListValues.Append(String.Format("{0}, ", Generator.GenerateEMailAddress(_DomainProps.DomainName, _DomainProps.CustomDomainName.ToString()))).ToString(); }
                                        break;
                                        case DomainType.Random:
                                        { _ListValues.Append(String.Format("'{0}', ", Generator.GenerateEMailAddress(_DomainProps.DomainName, ""))).ToString(); }
                                        break;
                                        case DomainType.Unique:
                                        { }
                                        break;
                                    }
                                }
                                break;
                                case ConfigurationPropertiesType.Encriptation:
                                {
                                    EncriptationProperties _EncripProps = (EncriptationProperties)_Field.Configuration;
                                    _ListValues.Append(String.Format("'{0}', ", Generator.GeneratePassword(_EncripProps.Encriptator, 16)).ToString());
                                }
                                break;
                                case ConfigurationPropertiesType.Int:
                                {
                                    IntProperties _IntProps = (IntProperties)_Field.Configuration;

                                    if (!_Field.PrimaryKey)
                                    {
                                        if (_IntProps.Consecutive)
                                        {
                                            if (_ListInt == null)
                                            {
                                                _ListInt = new List<int>();
                                                _ListInt = Generator.GenerateListInt(_IntProps.Start, _IntProps.End);
                                            }
                                            _ListValues.Append(String.Format("{0}, ", _ListInt[_i]));
                                            _i++;
                                        }
                                        else
                                        { _ListValues.Append(String.Format("{0}, ", Generator.GenerateInteger(_IntProps.Start, _IntProps.End))); }
                                    }
                                }
                                break;
                                case ConfigurationPropertiesType.String:
                                {
                                    StringProperties _StringPropr = (StringProperties)_Field.Configuration;
                                    switch (_StringPropr.ExpressionType)
                                    {
                                        case StringsExpressionsType.FULLNAME:
                                        { _ListValues.Append(String.Format("'{0}', ", Generator.GenerateFullName())); }
                                        break;
                                        case StringsExpressionsType.LASTNAME:
                                        { _ListValues.Append(String.Format("'{0}', ", Generator.GenerateLastName())); }
                                        break;
                                        case StringsExpressionsType.NAME:
                                        { _ListValues.Append(String.Format("'{0}', ", Generator.GenerateName())); }
                                        break;
                                        case StringsExpressionsType.TEXT:
                                        { _ListValues.Append(String.Format("'{0}', ", Generator.GenerateText())); }
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                        //Aqui es donde se crea la instruccion SQL
                        _Scripts.WriteLine(String.Format(_SQLInsert, TableName, _ListFields.ToString().Substring(0, (_ListFields.Length - 2)), _ListValues.ToString().Substring(0, (_ListValues.Length - 2))));
                        _ListValues.Remove(0, _ListValues.Length);
                        _ListFields.Remove(0, _ListFields.Length);
                    }
                    _Scripts.Close();
                    DBManager.PopulateDB(FileName);
                }
                else
                { throw new IndexOutOfRangeException(); }
            }
            catch (Exception)
            {
                _Scripts.Close();   
                throw;
            }

            
        }

        #endregion

	}

}
 